package com.richard.myapplication.fineViewPager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.richard.myapplication.R;
import com.richard.myapplication.smallViewpager.CommonPagerAdapter;
import com.richard.myapplication.smallViewpager.TransWithRotationTransformer;
import com.richard.myapplication.util.DisplayUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FineViewPagerActivity extends AppCompatActivity {
    private ViewPager vp_content;
    private List<Integer> colorList = Arrays.asList(R.color.colorPrimary, R.color.colorPrimaryDark, R.color.colorPrimary, R.color.colorAccent, R.color.color_red, R.color.bg_lunch_selected, R.color.bg_supper_selected);
    private List<Integer> colorsRes = new ArrayList<>(colorList);
    private List<String> texts = Arrays.asList("第一个","第二个","第三个","第四个","第五个","第六个","第⑦个");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fine_view_pager);
        vp_content = findViewById(R.id.vp_content);


        CommonPagerAdapter<Integer> horizontalStackAdapter = new CommonPagerAdapter<Integer>(colorsRes) {
            @Override
            public void renderItemView(@NonNull View itemView, final int position) {
                TextView tv_page_index = itemView.findViewById(R.id.tv_page_index);
                ((TextView) tv_page_index).setText(texts.get(position));
                int color = getResources().getColor(getBindItemData(position));
                GradientDrawable drawable = (GradientDrawable) tv_page_index.getBackground();
                if (drawable == null) {
                    drawable = new GradientDrawable();
                    tv_page_index.setBackground(drawable);
                }
                drawable.setCornerRadius(DisplayUtil.dp2px(getBaseContext(), 5));
                drawable.setStroke(DisplayUtil.dp2px(getBaseContext(), 5), getResources().getColor(R.color.color_light_grey));
                drawable.setColor(color);
            }

            @NonNull
            @Override
            public View getPageItemView(@NonNull ViewGroup container, int position) {
                View view = LayoutInflater.from(FineViewPagerActivity.this).inflate(R.layout.item_vp_fine_vp_pager, container, false);
                view.setTag(String.valueOf(position));
                return view;
            }
        };
        horizontalStackAdapter.setOnItemClickListener(new CommonPagerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getApplicationContext(), "当前点击第" + texts.get(position), Toast.LENGTH_SHORT).show();
            }
        });
        vp_content.setOffscreenPageLimit(4);
//        vp_content.setPageTransformer(false, new TransTransformer(vp_content));
        vp_content.setAdapter(horizontalStackAdapter);
        vp_content.setOverScrollMode(View.OVER_SCROLL_NEVER);
    }
}
